package find;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class test {

	   private static JFrame frame;
	    private static Point mouseDownCompCoords;
	    private static JToggleButton lockButton;
	    
	    // 调整后的选项卡透明度常量（Alpha值25对应约10%透明度）
	    private static final Color TAB_TRANSLUCENT = new Color(0, 0, 0, 25);
	    private static final Color BLACK_TRANSLUCENT = new Color(0, 0, 0, 25);
	    private static final Color ORIGINAL_BG = new Color(30, 30, 30, 25);

	    public static void main(String[] args) {
	        SwingUtilities.invokeLater(() -> createAndShowGUI());
	    }

    private static void createAndShowGUI() {
        frame = new JFrame("搜索窗口");
        frame.setUndecorated(true);
        frame.setBackground(new Color(0, 0, 0, 0)); // 完全透明窗体
        frame.setSize(300, 400);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mainPanel.setBackground(ORIGINAL_BG); // 主面板使用原始背景色
        frame.add(mainPanel);

        mainPanel.add(createSearchPanel(), BorderLayout.NORTH);
        mainPanel.add(createTabbedPane(), BorderLayout.CENTER);
        mainPanel.add(createBottomPanel(), BorderLayout.SOUTH);

        frame.setVisible(true);
    }

    // 创建搜索面板（包含搜索框和设置按钮）
    private static JPanel createSearchPanel() {
        JPanel panel = new JPanel(new BorderLayout(5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setBackground(ORIGINAL_BG); // 外层面板保持原始背景色

        // 搜索框容器面板 - 设置为黑色半透明
        JPanel searchContainer = new JPanel(new BorderLayout());
        searchContainer.setBackground(BLACK_TRANSLUCENT);  // 关键修改点1：搜索容器背景

        JTextField searchField = new JTextField();
        styleComponent(searchField);
        // 设置带透明效果的边框
        searchField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(100, 100, 100)), // 灰色边框
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        ));

        JButton searchButton = new JButton(new ImageIcon("./image/search.png"));
        styleComponent(searchButton);
        searchButton.setPreferredSize(new Dimension(40, 26));

        searchContainer.add(searchField, BorderLayout.CENTER);
        searchContainer.add(searchButton, BorderLayout.EAST);

        JButton settingsButton = new JButton("设置");
        styleComponent(settingsButton);
        settingsButton.setPreferredSize(new Dimension(60, 26));

        panel.add(searchContainer, BorderLayout.CENTER);
        panel.add(settingsButton, BorderLayout.EAST);

        addDragListeners(panel);
        return panel;
    }
    
    // 创建选项卡面板
    private static JTabbedPane createTabbedPane() {
        JTabbedPane tabbedPane = new JTabbedPane();
        styleComponent(tabbedPane);
        tabbedPane.setForeground(Color.WHITE);
        
        // 设置选项卡透明度为10%
        UIManager.put("TabbedPane.background", TAB_TRANSLUCENT);
        UIManager.put("TabbedPane.contentAreaColor", TAB_TRANSLUCENT);
        UIManager.put("TabbedPane.selected", new Color(80, 80, 80, 100)); // 可选：调整选中状态颜色
        SwingUtilities.updateComponentTreeUI(tabbedPane);

        // 创建列表（保持原有内容不变）
        DefaultListModel<String> listModel = new DefaultListModel<>();
        for (int i = 1; i <= 2; i++) {
            listModel.addElement("搜索结果项目 " + i);
        }

        JList<String> list = new JList<>(listModel);
        styleComponent(list);
        list.setBackground(TAB_TRANSLUCENT); // 同步列表背景透明度

        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());

        JPanel tab1 = new JPanel(new BorderLayout());
        tab1.setBackground(TAB_TRANSLUCENT);  // 内容区域同步透明度
        tab1.add(scrollPane, BorderLayout.CENTER);

        tabbedPane.addTab("主搜索", tab1);
        return tabbedPane;
    }

    // 创建底部拖动面板
    private static JPanel createBottomPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
        panel.setBackground(BLACK_TRANSLUCENT); // 关键修改点3：底部面板背景

        JLabel dragLabel = new JLabel("拖动区域");
        dragLabel.setForeground(Color.WHITE);
        panel.add(dragLabel, BorderLayout.WEST);

        lockButton = new JToggleButton("解锁");
        styleComponent(lockButton);
        lockButton.setPreferredSize(new Dimension(80, 25));
        lockButton.addActionListener(e -> {
            JToggleButton btn = (JToggleButton) e.getSource();
            btn.setText(btn.isSelected() ? "锁定" : "解锁");
        });
        panel.add(lockButton, BorderLayout.EAST);

        addDragListeners(panel);
        return panel;
    }

    // 组件样式统一设置方法
    private static void styleComponent(JComponent component) {
        component.setForeground(Color.WHITE);
        component.setBackground(ORIGINAL_BG); // 默认使用原始背景色

        // 特殊处理文本输入框
        if (component instanceof JTextField) {
            JTextField tf = (JTextField) component;
            tf.setCaretColor(Color.WHITE);
            tf.setOpaque(false); // 关键设置：使文本框透明
            tf.setBackground(BLACK_TRANSLUCENT); // 设置输入框背景为半透明黑
            tf.setSelectionColor(new Color(80, 80, 80)); // 文本选中颜色
        }

        // 按钮类组件样式
        if (component instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) component;
            button.setContentAreaFilled(false); // 透明背景
            button.setBorderPainted(false);     // 隐藏默认边框
            button.setFocusPainted(false);      // 隐藏焦点框
            button.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
        }

        // 列表组件特殊设置
        if (component instanceof JList) {
            component.setOpaque(false); // 使列表透明以显示父容器背景
        }
    }

    // 添加拖拽监听器
    private static void addDragListeners(JComponent comp) {
        comp.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (!lockButton.isSelected()) {
                    mouseDownCompCoords = e.getPoint();
                }
            }
        });

        comp.addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                if (mouseDownCompCoords != null && !lockButton.isSelected()) {
                    Point currCoords = e.getLocationOnScreen();
                    frame.setLocation(currCoords.x - mouseDownCompCoords.x,
                            currCoords.y - mouseDownCompCoords.y);
                }
            }
        });
    }
}